package org.flowers.unbeaten.util;

import java.util.Random;

/**
 * @description Token工具类
 * @author RyanWang
 * @date 2021-08-03 13:59:02
 * @version 1.0
 */
public class Token {

    /**
     * 创建随机盐
     * @param length
     * @return
     */
    public static String createSalt(int length) {
        return createRandomToken(4, false);
    }

    /**
     * 创建随机密码
     * @param length
     * @return
     */
    public static String createRandomPassword(int length) {
        return createRandomToken(length, true);
    }

    /**
     * 创建随机数字
     * @param length
     * @return
     */
    public static String createRandomNumber(int length) {
        return createRandomToken(4, true);
    }

    /**
     * 创建随机令牌
     * @param length
     * @param isNumberOnly
     * @return
     */
    public static String createRandomToken(int length, boolean isNumberOnly) {
        String base = isNumberOnly ? "0123456789" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        int max = base.length() - 1;

        StringBuilder token = new StringBuilder();
        for (int i = 0; i < length; i++) {
            token.append(base.charAt(random.nextInt(max)));
        }
        return token.toString();
    }

}
